package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.Bank;

import java.io.*;

/**
 * @author Gregory Sobko
 */
public class BankSaver {


    public static void saveBank(String path, Bank bank) throws IOException {
        try(FileOutputStream fileOutputStream = new FileOutputStream(path)){
            saveBank(fileOutputStream, bank);
        }
    }

    public static void saveBank(OutputStream outputStream,Bank bank) throws IOException {
        try(ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)){
            objectOutputStream.writeObject(bank);
        }
    }

    public static Bank readBank(String path) throws IOException, ClassNotFoundException {
        try(FileInputStream fileInputStream = new FileInputStream(path)){
            return readBank(fileInputStream);
        }
    }

    public static Bank readBank(InputStream inputStream) throws IOException, ClassNotFoundException {
        try(ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)){
            return (Bank)objectInputStream.readObject();
        }
    }
}
